/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatsoft.server.buz;

import ChatSoft.model.ChatMessage;
import ChatSoft.model.ConnectChatVoiceMessage;
import ChatSoft.model.FileRequestMessage;
import ChatSoft.model.FileResponseMessage;
import ChatSoft.model.UserData;
import ChatSoft.model.User;
import ChatSoft.model.VoiceRequestMessage;
import ChatSoft.model.VoiceResponseMessage;
import ChatSoft.model.buz.SendObject;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 *
 * @author KID
 */
public class ServerController extends Thread //Lien lac voi tung client cu the
{

    public ObjectOutputStream outStream;
    public ObjectInputStream inStream;
    public Socket svSocket;
    public Object recieveObject = null;
    private User user;
    private UserBussness userBussness;
    SendObject sendObject;
    UserData dataTranfer;
    ChatMessage chatMessage;
    private String UserName;
    ChatBussiness chatBussiness;

    public ServerController(Socket sk) {
        this.svSocket = sk;
        try {
            inStream = new ObjectInputStream(svSocket.getInputStream());
            outStream = new ObjectOutputStream(svSocket.getOutputStream());
            userBussness = new UserBussness();
            chatBussiness = new ChatBussiness();
        } catch (IOException ex) {
            System.out.println("Error!");
        }
    }

    @Override
    public void run() {
        Boolean result = false;
        try {
            while (true) {
                System.out.println("Liten Object....!");
                recieveObject = inStream.readObject();//nhan du lieu tu phia client!
                if (recieveObject instanceof UserData) {
                    dataTranfer = new UserData();
                    dataTranfer = (UserData) recieveObject;
                    //login
                    if (dataTranfer.getUser() != null && dataTranfer.getCommandName().equals("loginAccount")) {
                        UserName = dataTranfer.getUser().getUserName();
                        user = userBussness.userLogin(UserName, dataTranfer.getUser().getPassword());
                        dataTranfer = new UserData();
                        dataTranfer.setCommandName("loginResult");
                        dataTranfer.setUser(user);
                        if (user != null) {
                            ServerStaticVariable.userOnlineStream.put(user.getUserName(), outStream);// dua user vao danh sach online
                        }
                        SendObject.sendObject(dataTranfer, outStream);
                    } else
                        
                        //tao ra 1 tai khoan moi
                    if (dataTranfer.getUser() != null && dataTranfer.getCommandName().equals("createNewAccount")) {
                        result = userBussness.createNewUser(dataTranfer.getUser());
                        dataTranfer = new UserData();
                        dataTranfer.setResult(result);
                        dataTranfer.setCommandName("registerResult");
                        SendObject.sendObject(dataTranfer, outStream);
                    } else 
                        
                        //cap nhat tai khoan
                    if (dataTranfer.getUser() != null && dataTranfer.getCommandName().equals("updateAccount")) {
                        result = userBussness.updateAccount(dataTranfer.getUser());
                        dataTranfer.setResult(result);
                        dataTranfer.setCommandName("updateAccountResult");
                        SendObject.sendObject(dataTranfer, outStream);
                    } else 
                        
                        //kiem tra chat co' hop le ko?
                    if (dataTranfer.getRequestChatWith() != null && dataTranfer.getCommandName().equals("CheckChatRequest")) {
                        
                        dataTranfer.setCommandName("requestChatResult");
                        if (chatBussiness.CheckChatRequest(dataTranfer.getRequestChatWith())) {// nguoi can chat co online ko?
                            dataTranfer.setChatRequestResult(true);//neu co tra ve true, va san sang chat
                        } else {
                            dataTranfer.setChatRequestResult(false);// neu ko, tra ve false                            
                        }
                        SendObject.sendObject(dataTranfer, outStream);
                    }
                } //Chat
                else if (recieveObject instanceof ChatMessage) {
                    chatMessage = (ChatMessage) recieveObject;
                    chatBussiness.Chat(chatMessage);
                }
                else if (recieveObject instanceof FileRequestMessage) {
                    FileRequestMessage request = (FileRequestMessage) recieveObject;
                    chatBussiness.forwardFileRequest(request);
                }
                else if (recieveObject instanceof FileResponseMessage) {
                    FileResponseMessage response = (FileResponseMessage) recieveObject;
                    chatBussiness.forwardFileResponse(response);
                } 
                else if (recieveObject instanceof VoiceRequestMessage) {
                    VoiceRequestMessage request = (VoiceRequestMessage) recieveObject;
                    chatBussiness.forwardVoiceRequest(request);
                }
                else if (recieveObject instanceof VoiceResponseMessage) {
                    VoiceResponseMessage response = (VoiceResponseMessage) recieveObject;
                    chatBussiness.forwardVoiceResponse(response);
                } 
            }
        } catch (ClassNotFoundException ex) {
            System.out.println("error 4");
        } catch (IOException ex) {
            ServerStaticVariable.userOnlineStream.remove(UserName);
            System.out.println("User Disconnect!");
        }
    }
}